﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace wd_28_topmost_v1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer timer1 = new DispatcherTimer();
        public MainWindow()
        {
            InitializeComponent();
            timer1.Interval = new TimeSpan(0, 0, 1);
            //timer1.Tick += Timer1_tick;
            timer1.Tick += Timer2_tick;
            timer1.IsEnabled = true;
        }

        private void Timer2_tick(object sender, EventArgs e)
        {
            // Vysor.exe
            IntPtr vysorWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Chrome_WidgetWin_1", null);

            if (vysorWnd == GetForegroundWindow())
            {
                Trace.WriteLine("v主窗口置顶");
            }
            else
            {

            }
        }

        private void Timer1_tick(object sender, EventArgs e)
        {
            // 获取主窗口句柄
            IntPtr mainhwnd = new WindowInteropHelper(this).Handle;
            if (mainhwnd == GetForegroundWindow())
            {
                Trace.WriteLine("主窗口置顶");
            }
            else
            {

            }
            return;
        }

        public const int GWL_STYLE = (-16);
        private const int GWL_EXSTYLE = -20;
        private const int WS_EX_NOACTIVATE = 0x08000000;
        public const int WS_THICKFRAME = 0x40000;
        private const int WS_EX_TOPMOST = 0x00000008;
        const int SWP_NOSIZE = 0x1;
        const int SWP_NOMOVE = 0x2;
        const int SWP_NOZORDER = 0x4;
        const int SWP_FRAMECHANGED = 0x20;

        public bool isWndTopMost(IntPtr hwnd)
        {
            //GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST;
            GetWindowLong(hwnd, GWL_EXSTYLE);
            return false;
        }

        public static IntPtr GetWindowLong(IntPtr hWnd, int nIndex)
        {
            return Environment.Is64BitProcess
                ? GetWindowLong64(hWnd, nIndex)
                : GetWindowLong32(hWnd, nIndex);
        }

        public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
        {
            return Environment.Is64BitProcess
                ? SetWindowLong64(hWnd, nIndex, dwNewLong)
                : SetWindowLong32(hWnd, nIndex, dwNewLong);
        }

        [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
        private static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
        private static extern IntPtr GetWindowLong64(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
        private static extern IntPtr SetWindowLong32(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
        private static extern IntPtr SetWindowLong64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        // 
        // ————————————————
        // 版权声明：本文为CSDN博主「walter lv」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
        // 原文链接：https://blog.csdn.net/WPwalter/article/details/104573308
        //获取当前活动窗口(当前焦点窗口,键盘可操作的那种)
        [DllImport("user32.dll")]
        static extern IntPtr GetActiveWindow();

        //获取当前置顶窗口(窗口总在最前,类似于QQ那种)
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
        //返回值类型是指针，用作其他函数获取信息

        //根据窗口类型或者窗口标题获取窗口句柄
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        //根据父窗口句柄、子窗口句柄、窗口类型、窗口标题 获取句柄
        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    }
}
